//
//  ISwitchViewController.m
//  iSwitchView
//
//  Created by Ronnie on 08-11-4.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "MainWindowController.h"

@implementation MainWindowController

- (id) init
{
	self = [super init];
	if (self != nil) {
		currentViewController = nil;
		loginView = nil;
		listView = nil;
	}
	return self;
}

- (void) dealloc
{
	[currentViewController release];
	[loginView release];
	[listView release];
	[loginCtrl release];
	[listCtrl release];
	[super dealloc];
}


- (void)awakeFromNib
{	
	// At begin, we should init the login view
	loginCtrl = [[LoginController alloc] initWithNibName:@"login" bundle:nil];
	[loginCtrl setMainWindowController:self];
	if (currentViewController) {
		[currentViewController release];
	}
	currentViewController = [loginCtrl retain];
	
	loginView = [[currentViewController view] retain];
	
	[self switchToView:loginView];
	
	// add notifications
	[self addNotificationResponser];
	
	// set the main application controller
	mainAppCtrl = [[MainAppController alloc] init];
	[mainAppCtrl setMainWindowController:self];
	[loginCtrl setMainAppController:mainAppCtrl];
}

- (void)processSwitchToListViewNotication:(NSNotification *)notification
{
	[self performSelectorOnMainThread:@selector(switchToListView) withObject:nil waitUntilDone:nil];
}
- (void)switchToListView
{
	// init the list controller
	listCtrl = [[ListController alloc] initWithNibName:@"ListView" bundle:nil];
	
	// set the fxdata to list controller
	if (currentViewController)
		[currentViewController release];
	currentViewController = [listCtrl retain];
	
	//NSWindow* window = [contentView window];
	listView = [[currentViewController view] retain];
	
	// set the fxdata to list controller
	[listCtrl setMainWindowController:self];
	[listCtrl setMainAppController:mainAppCtrl];
	
	[self switchToView:listView];
	//[self performSelectorOnMainThread:@selector(switchToView:) withObject:listView waitUntilDone:nil];

	[[[self window] toolbar] setVisible:YES];
	[[[[self window] toolbar] delegate] setMainWindowController:self];
	[[[[self window] toolbar] delegate] setMainAppController:mainAppCtrl];
}

- (void)switchToView:(NSView*)newView
{
	NSView* aView = [newView retain];
	
	NSSize contentViewSize = [[[self window] contentView] frame].size;
	NSSize newViewSize = [aView frame].size;
	float deltaWidth = newViewSize.width - contentViewSize.width;
	float deltaHeigh = newViewSize.height - contentViewSize.height;
	
	NSRect windowFrame = [[self window] frame];
	windowFrame.size.height += deltaHeigh;
	windowFrame.size.width += deltaWidth;
	windowFrame.origin.y -= deltaHeigh;
	
	[[self window] setContentView:nil];
	[[self window] setFrame:windowFrame display:YES	animate:YES];
	[[self window] setContentView:aView];
}

- (ListController*)getListController
{
	return listCtrl;
}

- (LoginController*)getLoginController
{
	return loginCtrl;
}

#pragma mark method for notifications

- (void)addNotificationResponser
{
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(processSwitchToListViewNotication:)
												 name:@"FETION_HAVE_LOGIN" object:nil];
}

- (BOOL)windowShouldClose:(id)sender {	
	// if hide on close
	[[self window] orderOut:self];
	return NO;
}

- (BOOL) applicationShouldHandleReopen: (id) s hasVisibleWindows: (BOOL) b {
    [[self window] makeKeyAndOrderFront: self];
    return NO;
} 

- (void)windowDidResize:(NSNotification *)notification
{
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	NSLog(@"Main Window Resize");
	[nc postNotificationName:@"MAIN_WINDOW_RESIZE" object:nil];
}

@end
